package lumis.service.wsrp.producer;

import javax.persistence.NoResultException;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;

/**
 * {@link ProcessActionHandler} responsible for deletion of {@link RemoteConsumerRegistration}
 * from a {@link ProducerImpl} associated with current service instance.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class RegistrationAdministrationListDeleteProcessActionHandler extends ProcessActionHandler<TabularSource<ITabularSourceField>>
{

	public void processAction() throws PortalException
	{
		String[] registrationHandles = getParameter("registrationHandle", String[].class);
		if (registrationHandles != null && registrationHandles.length > 0)
		{
			
			for (String registrationHandle : registrationHandles)
			{
				try
				{
					RemoteConsumerRegistration registration = (RemoteConsumerRegistration) ManagerFactory.getEntityManager().find(RemoteConsumerRegistration.class, registrationHandle);
					ProducerImpl producer = registration.getProducer();
					producer.getRegistrations().remove(registration);
					ManagerFactory.getEntityManager().remove(registration);
				}
				catch (NoResultException e)
				{
					//ignores registrationHandles not found
				}
			}
		}
		addDefaultResponse();
	}
}
